/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.agilelang.config;

import org.agilelang.config.AgileStructure;
import org.agilelang.config.AgileSyntax;
import org.agilelang.config.AgileConfig;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.agilelang.config.FileStatic;
import org.agilelang.main.Main;
import org.agilelang.object.ObjectClass;
import org.agilelang.object.ObjectFunction;
import org.agilelang.object.ObjectParam;
import org.agilelang.object.ObjectProject;
import org.agilelang.object.Read;

/**
 *
 * @author RoderickNestor
 */
public class ReadConfig {

    private AgileConfig aconfig = new AgileConfig();
    private AgileStructure astructure = new AgileStructure();
    private HashMap<String, AgileSyntax> hashAgileSyntax = new HashMap<>();
    List<ObjectClass> listObjectClass = new ArrayList<>();

    public List<ObjectClass> getListObjectClass() {
        return listObjectClass;
    }

    public void setListObjectClass(List<ObjectClass> listObjectClass) {
        this.listObjectClass = listObjectClass;
    }

    public void process(String path_xml) {

        Read rd = new Read();
        ObjectProject agileproject = rd.process(path_xml);
        for (ObjectClass cls : agileproject.getClasses()) {

            if (cls.getType().equals("AgileConfig")) {
                List<ObjectFunction> functions = cls.getFunctions();
                for (ObjectFunction function : functions) {
                    switch (function.getName()) {
                        case "setProjectName": {
                            List<ObjectParam> params = function.getParams();
                            for (ObjectParam param : params) {
                                if (param.getName().equals("name")) {
                                    aconfig.projectName = param.getValue();
                                }
                            }
                            break;
                        }
                        case "addFolderTemplate": {
                            List<ObjectParam> params = function.getParams();
                            for (ObjectParam param : params) {
                                if (param.getName().equals("pathname")) {
                                    aconfig.folderTemplate = param.getValue();
                                }
                            }
                            break;
                        }
                        case "addFolderStatic": {
                            List<ObjectParam> params = function.getParams();
                            for (ObjectParam param : params) {
                                if (param.getName().equals("pathname")) {
                                    aconfig.folderStatic = param.getValue();
                                }
                            }
                            break;
                        }
                        case "addFolderOut": {
                            List<ObjectParam> params = function.getParams();
                            for (ObjectParam param : params) {
                                if (param.getName().equals("pathname")) {
                                    aconfig.folderOut = param.getValue();
                                }
                            }
                            break;
                        }
                        case "addFolderResource": {
                            List<ObjectParam> params = function.getParams();
                            for (ObjectParam param : params) {
                                if (param.getName().equals("pathname")) {
                                    aconfig.folderResource = param.getValue();
                                }
                            }
                            break;
                        }
                    }
                }

            }
            if (cls.getType().equals("AgileStructure")) {
                List<ObjectFunction> functions = cls.getFunctions();
                for (ObjectFunction function : functions) {
                    switch (function.getName()) {
                        case "addFolder": {
                            List<ObjectParam> params = function.getParams();
                            for (ObjectParam param : params) {
                                if (param.getName().equals("pathname")) {
                                    astructure.folder.add(param.getValue());
                                }
                            }
                            break;
                        }
                        case "addFile": {
                            List<ObjectParam> params = function.getParams();
                            FileStatic file = new FileStatic();
                            for (ObjectParam param : params) {
                                if (param.getName().equals("input")) {
                                    file.input = param.getValue();
                                }
                                if (param.getName().equals("output")) {
                                    file.output = param.getValue();
                                }
                            }
                            astructure.file.add(file);
                            break;
                        }
                        case "addZip": {
                            List<ObjectParam> params = function.getParams();
                            FileStatic file = new FileStatic();
                            for (ObjectParam param : params) {
                                if (param.getName().equals("input")) {
                                    file.input = param.getValue();
                                }
                                if (param.getName().equals("output")) {
                                    file.output = param.getValue();
                                }
                            }
                            astructure.zip.add(file);
                            break;
                        }

                    }
                }
            }
            if (cls.getType().equals("AgileSyntax")) {
                System.out.println(cls.getName() + " -- " + cls.getType());
                
                cls.setType(cls.getClassattributes().get(0).getValue());
                listObjectClass.add(cls);
                AgileSyntax aSyntax = null;
                if (hashAgileSyntax.containsKey(cls.getName())) {
                    aSyntax = hashAgileSyntax.get(cls.getName());
                } else {
                    aSyntax = new AgileSyntax();
                }
                List<ObjectFunction> functions = cls.getFunctions();
                for (ObjectFunction function : functions) {
                    switch (function.getName()) {
                        case "defineTemplate": {
                            List<ObjectParam> params = function.getParams();
                            for (ObjectParam param : params) {
                                if (param.getName().equals("name")) {
                                    aSyntax.name = param.getValue();
                                }
                                if (param.getName().equals("ext")) {
                                    aSyntax.ext = param.getValue();
                                }
                                if (param.getName().equals("template")) {
                                    aSyntax.template = param.getValue();
                                }
                                if (param.getName().equals("filename")) {
                                    aSyntax.filename = param.getValue();
                                }
                                if (param.getName().equals("extend")) {
                                    aSyntax.extend = param.getValue();
                                }
                            }
                            break;
                        }
                    }
                }
                hashAgileSyntax.put(cls.getName(), aSyntax);
            }
        }
    }

    public static void main(String args[]) {
        //-create structure.xml
        ReadConfig main = new ReadConfig();
        main.process("E:\\agile\\AgileLang\\Test\\out.xml");

    }

    public AgileConfig getAconfig() {
        return aconfig;
    }

    public void setAconfig(AgileConfig aconfig) {
        this.aconfig = aconfig;
    }

    public AgileStructure getAstructure() {
        return astructure;
    }

    public void setAstructure(AgileStructure astructure) {
        this.astructure = astructure;
    }

    public HashMap<String, AgileSyntax> getHashAgileSyntax() {
        return hashAgileSyntax;
    }

    public void setHashAgileSyntax(HashMap<String, AgileSyntax> hashAgileSyntax) {
        this.hashAgileSyntax = hashAgileSyntax;
    }
}
